import { Controller } from 'egg';

class BrowseController extends Controller {
    /**
     * 计算浏览时间
     */
    public async computeBrowseTime() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                user_id: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const { user_id } = request.body;
        await ctx.service.browseService.computeBrowseTime(user_id);
        ctx.body = ctx.helper.responseSuccess('更新浏览记录成功');
    }

    /**
     * 获取每日首次点击量
     */
    public async getClickNum() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                startTime: 'string',
                endTime: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.browseService.getClickNum(request.body);
        ctx.body = ctx.helper.responseSuccess('获取成功', result);
    }

    /**
     * 获取用户日浏览平均时间
     */
    public async getBrowseTime() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                startTime: 'string',
                endTime: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.browseService.getBrowseTime(request.body);
        ctx.body = ctx.helper.responseSuccess('获取成功', result);
    }
}
export default BrowseController;